package com.github.caijh.mall.upload.controller;

import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.system.service.SystemAttachmentService;
import com.github.caijh.mall.upload.service.UploadService;
import com.github.caijh.mall.upload.vo.FileResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("api/admin/upload")
@Api(tags = "上传文件")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    /**
     * 上传图片.
     *
     * @param multipart 文件
     * @param model     模块
     * @param pid       上一级id
     * @return FileResultVo
     */
    @ApiOperation(value = "图片上传")
    @PostMapping(value = "/image")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "模块 用户user,商品product,微信wechat,news文章"),
            @ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列 ", allowableValues = "range[0,1,2,3,4,5,6,7,8]")
    })
    public FileResultVo image(MultipartFile multipart,
                              @RequestParam(value = "model") String model,
                              @RequestParam(value = "pid") Integer pid) {
        return uploadService.image(multipart, model, pid);
    }

    @ApiOperation(value = "文件上传")
    @PostMapping(value = "/file")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "模块 用户user,商品product,微信wechat,news文章", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列 ",
                    dataTypeClass = Integer.class,
                    allowableValues = "range[0,1,2,3,4,5,6,7,8]")
    })
    public CommonResult<FileResultVo> file(MultipartFile multipart,
                                           @RequestParam(value = "model") String model,
                                           @RequestParam(value = "pid") Integer pid) {
        return CommonResult.success(uploadService.file(multipart, model, pid));
    }

    /**
     * 同步到云.
     */
    @ApiOperation(value = "同步到云服务")
    @PostMapping(value = "/async")
    public void async() {
        systemAttachmentService.async();
    }

}



